package net.suncaper.demo.controller;

import net.suncaper.demo.domain.Product;
import net.suncaper.demo.domain.Shopcart;
import net.suncaper.demo.mapper.ShopcartMapper;
import net.suncaper.demo.service.ProductService;
import net.suncaper.demo.service.ShopcartService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.StringReader;
import java.util.List;

@Controller
@RequestMapping
public class Shopcartcontroller {
    @Autowired
    private ShopcartService shopcartService;
    private ProductService productService;
    //添加到购物车
    @GetMapping("/addShopcart")
    public String shopcartPage(){
        return "pages/shopcart/add-shopcart.html";
    }

    @PostMapping("/shopcart/addShopcart")
    public String saveAddShopcart(Shopcart shopcart){
        shopcartService.saveAddShopcartMes(shopcart);
        return "redirect:/shopcart";
    }

    //修改购物车信息
    @GetMapping("/shopcart/updateShopcart")
    public String editPage(int shopcartId,Model model){
        model.addAttribute("shopcart",shopcartService.findShopcartBySId(shopcartId));
        return "pages/shopcart/edit-shopcart.html";
    }

    @PostMapping("/updateShopcart")
    public String saveUpdateShopcart(Shopcart shopcart){
        shopcartService.saveUpdateShopcartMes(shopcart);
        return "redirect:/shopcart";
    }

    //删除购物车商品
//    @GetMapping("/delShopcart")
//    public String delShopcartBySId(int shopcartId, Shopcart shopcart,Model model){
//        shopcartService.delShopcartBySIdMes(shopcartId);
//        List<Shopcart> shopcarts1 = shopcartService.findShopcartByUId(shopcart.getUserId());
//        model.addAttribute("shopcarts",shopcarts1);
//        //return "redirect:/shopcart";
//        return  "pages/shopcart/shopcart.html";
//    }

    @GetMapping("/shopcart/delShopcart")
    public String delPage(int shopcartId,Model model){
        model.addAttribute("shopcart",shopcartService.findShopcartBySId(shopcartId));
        return "pages/shopcart/del-shopcart.html";
    }
    @PostMapping("/shopcart/delShopcart")
    public String delShopcartBySId(Shopcart shopcart){
        shopcartService.delShopcartBySIdMes(shopcart.getShopcartId());
        return "pages/user/index.html";
    }


    //显示购物车列表，查看我的购物车
    @RequestMapping("/searchShopcart")
    @ResponseBody
    public List<Shopcart> findShopcart(){
        return shopcartService.findShopcart();
    }

    @RequestMapping("/shopcart")
    public String shopcartPage(Model model){
        List<Shopcart>shopcarts=shopcartService.findShopcart();
        model.addAttribute("shopcarts",shopcarts);
        return "pages/shopcart/shopcart.html";
    }

    //通过用户user_id查询与其相关的购物车信息
    @RequestMapping("/searchShopcartByUid")
    @ResponseBody
    public List<Shopcart> findShopcartByUid(int userId){
        return shopcartService.findShopcartByUId(userId);
    }

    @GetMapping ("/shopcart/selectByUid")
    public String shopcartSelectByUid(String userId,Model model){
        int userID = Integer.valueOf(userId).intValue();
        List<Shopcart> shopcartsbyuid = shopcartService.findShopcartByUId(userID);
        model.addAttribute("shopcarts",shopcartsbyuid);
        return "pages/shopcart/shopcart.html";
    }

    @PostMapping("/shopcart/addToShopcart")
    public String shopcartPagebyuid(Shopcart shopcart, Model model){
        int userId=shopcart.getUserId();
        int tempproductid = shopcart.getProductId();
        int tempproductnum = shopcart.getShopcartNum();
        int temp =0;
        String tempproductname = shopcart.getProductName();
        List<Shopcart> shopcarts = shopcartService.findShopcartByUId(userId);
        for(Shopcart shopcart1:shopcarts){
            if(shopcart1.getProductId()==tempproductid){
                shopcart1.setShopcartNum(shopcart1.getShopcartNum()+tempproductnum);
                shopcart1.setProductName(tempproductname);
                shopcartService.saveUpdateShopcartMes(shopcart1);
                temp=1;
            }
        }
        if(temp==0){
            shopcartService.saveAddShopcartMes(shopcart);
        }

//        shopcartService.saveAddShopcartMes(shopcart);
//        System.out.println(shopcart.getUserId());
        List<Shopcart> shopcarts1 = shopcartService.findShopcartByUId(shopcart.getUserId());
       model.addAttribute("shopcarts",shopcarts1);
       return "pages/shopcart/shopcart.html";
    }

    @RequestMapping("/decreaseNum")
    public String decreaseNum(Model model,Integer shopcart_id,String userId){
        int userID = Integer.valueOf(userId).intValue();
        shopcartService.decreaseNum(shopcart_id);
        List<Shopcart> shopcarts =shopcartService.findShopcartByUId(userID);
        model.addAttribute("shopcarts",shopcarts);
        return "pages/shopcart/shopcart.html";
    }
    @RequestMapping("/increaseNum")
    public String increaseNum(Model model,Integer shopcart_id,String userId){
        int userID = Integer.valueOf(userId).intValue();
        shopcartService.increaseNum(shopcart_id);
        List<Shopcart> shopcarts =shopcartService.findShopcartByUId(userID);
        model.addAttribute("shopcarts",shopcarts);
        return "pages/shopcart/shopcart.html";
    }
}
